using System;
using Scenario.WorkflowEngine.DataModel;

namespace Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData
{
	public class XMLApplication : bOS.XML.Data.XMLObject  
	{
		public const string MAIN_NODE				= "Application";
		
		public const string NAME_ATTRIBUTE			= "Name";
		public const string TYPE_ATTRIBUTE			= "Type";
		public const string LIBRARYFILE_ATTRIBUTE	= "LibraryFile";

		public const string HOST_NODE				= "Host";
		public const string PORT_NODE				= "Port";

		public const string  EXECUTABLE_TYPE		= "EXECUTABLE";
		public const string  PROCESS_TYPE			= "EXTERNAL_PROCESS";
		public const string  LIBRARY_TYPE			= "LIBRARY";
		public const string  COMEXECUTABLE_TYPE		= "ComExecutable";
		public const string  CORBAEXECUTABLE_TYPE	= "CorbaExecutable";

		/** FIELDS **/

		private string sName;
		private string sType;
		private string sLibraryFile;

		private XMLApplicationFunctions vFunctions;
		private Common.XMLExtendedAttributes vExtendedAttributes;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLApplication()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLApplication(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		/** PROPERTIES **/


		public string Name 
		{
			get { return sName; }
			set { sName = value; }
		}

		public string Type 
		{
			get { return sType; }
			set { sType = value; }
		}

		public string LibraryFile 
		{
			get { return sLibraryFile; }
			set { sLibraryFile = value; }
		}

		public XMLApplicationFunctions XMLApplicationFunctions 
		{
			get { return vFunctions; }
			set { vFunctions = value; }
		}

		public Common.XMLExtendedAttributes XMLExtendedAttributes 
		{
			get { return vExtendedAttributes; }
			set { vExtendedAttributes = value; }
		}

		/** INSTANCE METHODS **/

		override public void Init()
		{
			sName= string.Empty;
			sType= string.Empty;
			sLibraryFile= string.Empty;
			vFunctions = new XMLApplicationFunctions();
			vExtendedAttributes = new Common.XMLExtendedAttributes();
		}

		/** Parsering method to fill the object **/
		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);
				switch ( node.Name ) 
				{
					case XMLApplication.NAME_ATTRIBUTE:
						sName = node.InnerText;
						break;
					case XMLApplication.TYPE_ATTRIBUTE:
						sType = node.InnerText;
						break;
					case XMLApplication.LIBRARYFILE_ATTRIBUTE:
						sLibraryFile = node.InnerText;
						break;
				}
			}

			/** find the attributes situated on sub tags **/
			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch ( node.Name )
				{
					case XMLApplicationFunctions.MAIN_NODE:
						vFunctions.Parse(node);
						break;
					case Common.XMLExtendedAttributes.MAIN_NODE:
						vExtendedAttributes.Parse(node);
						break;
				}
			}
		}

		/**Method to retive a node from object **/
		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLApplication.MAIN_NODE);
			xmlNode.AddAttribute ( new bOS.XML.XMLAttribute ( XMLApplication.NAME_ATTRIBUTE, sName) );
			xmlNode.AddAttribute ( new bOS.XML.XMLAttribute  ( XMLApplication.TYPE_ATTRIBUTE, sType) );
			xmlNode.AddAttribute ( new bOS.XML.XMLAttribute  ( XMLApplication.LIBRARYFILE_ATTRIBUTE, sLibraryFile) );

			xmlNode.AddNode ( vExtendedAttributes.GetXMLNode() );
			xmlNode.AddNode ( vFunctions.GetXMLNode() );
			
			return xmlNode;
		}
	}
}